import vueLazyLoad from "vue-lazyload";
import ElementUI from "element-ui";
import $scroll from "@utils/loading";
import MD5 from "js-md5";
import "element-ui/lib/theme-chalk/index.css";
import cookie from "@utils/store/cookie";
import localStorage from "@utils/store/localStorage";
import i18n from "@plugins/i18n";
import cache from "./cache";
import "@assets/css/view.css";
import "@assets/css/iconfont.css";
import {
    Button,
    Cell,
    CellGroup,
    Checkbox,
    CheckboxGroup,
    DatetimePicker,
    Field,
    Image,
    List,
    NavBar,
    NumberKeyboard,
    Overlay,
    PasswordInput,
    Picker,
    Popover,
    Popup,
    PullRefresh,
    Radio,
    RadioGroup,
    Search,
    Slider,
    Swipe,
    SwipeItem,
    Switch,
    Tab,
    Tabs,
    Toast,
    Uploader
} from "vant";

const MESSAGE_TYPE = {
    w: "warning",
    e: "error",
    i: "info",
    s: "success"
};

const JUST_NOW = 3000; //3s内
const IN_SECOND = 1000 * 60; //一分钟
const IN_MINUTE = 1000 * 60 * 60; //一小时
const IN_HOUR = 1000 * 60 * 60 * 12; //12小时
const IN_DAY = 1000 * 60 * 60 * 24; //1天
const IN_MONTH = 1000 * 60 * 60 * 24 * 30; //1个月

export default {
    install(Vue) {
        Vue.config.productionTip = false;
        Vue.config.devtools = true;
        Vue.use(vueLazyLoad, {
            preload: 1.3, //加载高度比。
            loading: require("@assets/img/base/noPictrue.png"), //加载时的过渡图片
            error: require("@assets/img/base/err.png"), //加载失败的图片
            attempt: 2 //每次加载的张数。
        });
        Vue.use(PullRefresh);
        Vue.use(ElementUI);
        Vue.use(NavBar);
        Vue.use(Popup);
        Vue.use(Swipe);
        Vue.use(SwipeItem);
        Vue.use(Image);
        Vue.use(Cell);
        Vue.use(CellGroup);
        Vue.use(Field);
        Vue.use(RadioGroup);
        Vue.use(Radio);
        Vue.use(NumberKeyboard);
        Vue.use(PasswordInput);
        Vue.use(Button);
        Vue.use(Slider);
        Vue.use(CheckboxGroup);
        Vue.use(Checkbox);
        Vue.use(Uploader);
        Vue.use(Picker);
        Vue.use(Overlay);
        Vue.use(List);
        Vue.use(Tabs);
        Vue.use(Tab);
        Vue.use(Toast);
        Vue.use(DatetimePicker);
        Vue.use(Search);
        Vue.use(Popover);
        Vue.use(Switch);
        Vue.prototype.$scroll = $scroll;
        Vue.prototype.$cache = cache;
        Vue.prototype.$md5 = MD5;
        Vue.prototype.$cookie = cookie;
        Vue.prototype.$storage = localStorage;
        Vue.mixin({
            methods: {
                processMessage(params) {
                    Object.keys(params).forEach(k => {
                        if (k.startsWith("msg_")) {
                            let type = k.split("_");
                            if (type.length !== 2 || !MESSAGE_TYPE[type[1]]) return;
                            this.$message({
                                message: params[k],
                                type: MESSAGE_TYPE[type[1]],
                                center: true
                            });
                        }
                    });
                }
            }
        });
        Date.prototype.toChatMessage = function () {
            let localTime = new Date(); //当前系统时间
            let createTime = this; //消息创建时间
            let diff = localTime - createTime;
            if (diff <= JUST_NOW) return i18n.t("time.just");
            else if (diff <= IN_MINUTE)
                return i18n.t("time.minute", {minute: Math.ceil(diff / IN_SECOND)});
            else {
                const list = createTime.toString().split(" ");
                let year = createTime.getFullYear().toString(),
                    month = (createTime.getMonth() + 1).toString(),
                    day = createTime.getDate().toString(),
                    week = createTime.getDay();
                const lastIndex = list[4].lastIndexOf(":");
                let realtime = list[4].toString().substring(0, lastIndex);
                if (diff <= IN_HOUR * 2) return realtime;
                else if (diff < IN_DAY * 7) {
                    return i18n.t("time.week_" + week) + " " + realtime;
                } else if (diff < IN_MONTH * 12) {
                    if (localTime.getFullYear().toString() !== year) {
                        realtime =
                            year +
                            "-" +
                            month.padStart(2, "0") +
                            "-" +
                            day.padStart(2, "0") +
                            " " +
                            realtime;
                    } else {
                        realtime = month + "-" + day + " " + realtime;
                    }
                    return realtime;
                }
            }
        };
        Date.prototype.format = function (format) {
            let date = this;
            if (!format) format = "yyyy-MM-dd";
            let dict = {
                yyyy: date.getFullYear(),
                M: date.getMonth() + 1,
                d: date.getDate(),
                H: date.getHours(),
                m: date.getMinutes(),
                s: date.getSeconds(),
                MM: ("" + (date.getMonth() + 1)).padStart(2, "0"),
                dd: ("" + date.getDate()).padStart(2, "0"),
                HH: ("" + date.getHours()).padStart(2, "0"),
                mm: ("" + date.getMinutes()).padStart(2, "0"),
                ss: ("" + date.getSeconds()).padStart(2, "0"),
                SSS: ("" + date.getMilliseconds()).padStart(3, "0")
            };
            return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?|SSS?)/g, function () {
                return dict[arguments[0]];
            });
        };
        Number.prototype.amountFormat = function (accuracy) {
            let value = accuracy && accuracy > 0 ? this.toFixed(accuracy) : this;
            return (value || 0).toString().replace(/\d+/, function (n) {
                let len = n.length;
                if (len % 3 === 0) {
                    return n.replace(/(\d{3})/g, ",$1").slice(1);
                } else {
                    return (
                        n.slice(0, len % 3) + n.slice(len % 3).replace(/(\d{3})/g, ",$1")
                    );
                }
            });
        };
        String.prototype.amountFormat = function (accuracy) {
            if (this === "") return "0";
            return parseInt(this).amountFormat(accuracy);
        };
        String.prototype.toPrivacy = function (length) {
            if (this.length < 2) return "***";
            if (this.length < length) length = this.length;
            return (
                this.substring(0, length) + "***" + this.substring(this.length - length)
            );
        };
        String.prototype.startsWithFromArray = function (searchArray) {
            if (!searchArray || searchArray.length === 0) return false;
            for (let i in searchArray) {
                if (this.startsWith(searchArray[i])) return true;
            }
            return false;
        }
        String.prototype.replaceAllFromArray = function (searchArray, replaceValue) {
            if (!searchArray || searchArray.length === 0) return;
            for (let i in searchArray) {
                if (this.startsWith(searchArray[i]))
                    return this.replaceAll(searchArray[i], replaceValue);
            }
            return this;
        }
    }
};
